﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses.Data_Adapters;
namespace CECB
{
    /// <summary>
    /// Interaction logic for Team_Window.xaml
    /// </summary>
    public partial class Team_Window : Window
    {
        public Team_Window()
        {
            InitializeComponent();
            listBoxTeamMembers.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getConsultents();
            combobox_TeamLeader.ItemsSource = new cecbClasses.Data_Adapters.Func_ProjectTeams().getTeamLeaders();
            TeamAutocompleteTextBox.ItemsSource = new cecbClasses.Data_Adapters.Slider().getTeamDetails();
            dataContext = new DSSEntities();
        }
        DSSEntities dataContext;
        private void button_Save_Member_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void button_Save_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public Boolean validateTeamName(String name) {
            List<Teams> listTeam = TeamAutocompleteTextBox.ItemsSource.OfType<Teams>().ToList();
            Boolean isValide = true;
            foreach (Teams team in listTeam) {

                if (name == team.team_name)
                    isValide = false;
                
            }
            return isValide;
        
        
        }


        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
             
            if (TeamAutocompleteTextBox.Text.Length == 0)
            {
                MessageBox.Show("Enter Team Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                
            }
            else if (!validateTeamName(TeamAutocompleteTextBox.Text.ToString()))
            {
                MessageBox.Show("Team Name Already Exist, Enter New Team Name", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (combobox_TeamLeader.SelectedIndex == -1)
            {
                MessageBox.Show("Select Team Leader", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                String memberId = combobox_TeamLeader.SelectedValue.ToString();

                int size = listBoxTeamMembers.Items.Count;
                List<Members> list = new List<Members>();
                List<int> spectList = new List<int>();
                foreach (Members s in listBoxTeamMembers.SelectedItems)
                {
                    list.Add(s);
                    
                    spectList.Add(1);
                }
                 

                cecbClasses.Data_Adapters.Func_ProjectTeams ptm = new cecbClasses.Data_Adapters.Func_ProjectTeams();
                ptm.InsertProjectTeam(TeamAutocompleteTextBox.Text.Trim(), list, memberId, spectList);

                MessageBox.Show("Team Created Successfully", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                clear();
                listBoxTeamMembers.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getConsultents();
                combobox_TeamLeader.ItemsSource = new cecbClasses.Data_Adapters.Func_ProjectTeams().getTeamLeaders();
            }

        }

        public void clear() 
        {
            TeamAutocompleteTextBox.ItemsSource = new cecbClasses.Data_Adapters.Slider().getTeamDetails();
            combobox_TeamLeader.SelectedIndex = -1;
            listBoxTeamMembers.ItemsSource = null;
            listBoxTeamMembers.Items.Clear();
            TeamAutocompleteTextBox.Text = "";
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void comboboxMemSpec_DropDownOpened(object sender, EventArgs e)
        {
            if (listBoxTeamMembers.SelectedIndex > -1)
            {
                Members mem = (Members)listBoxTeamMembers.SelectedItem;
                ComboBox cmb = (ComboBox)sender;
                //cmb.ItemsSource = from c data
                List<MemberSpecilizations> spec = (from c in dataContext.Members
                                                   join d in dataContext.MemberSpecilizations on c.mem_reference equals d.mem_reference
                                                   where c.mem_reference == mem.mem_reference
                                                   select d).ToList();
                
            }
            else {
                MessageBox.Show("Select a member", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void TeamReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
